﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace UnnamedPodcastLibrary
{
    [DebuggerDisplay("{Title}")]
    public class Episode : IEquatable<Episode>
    {
        public Episode(string uniqueId, Uri downloadUrl, Uri feed, string episodeTitle)
        {
            if (string.IsNullOrEmpty(uniqueId)) throw new ArgumentException("uniqueId");
            if (downloadUrl == null) throw new ArgumentException("downloadUrl");
            if (feed == null) throw new ArgumentException("feed");
            if (string.IsNullOrEmpty(episodeTitle)) throw new ArgumentException("episodeTitle");

            this.UniqueId = uniqueId;
            this.DownloadUrl = downloadUrl;
            this.Feed = feed;
            this.Title = episodeTitle;
        }

        public string UniqueId { get; set; }
        public Uri DownloadUrl { get; set; }

        string _state;
        public string State 
        { 
            get { return this._state; } 
            set 
            {
                if (!EpisodeState.IsValid(value))
                    throw new ArgumentOutOfRangeException();
                this._state = value; 
            } 
        }

        public Uri Feed { get; set; }
        public string FeedTitle { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public DateTime? DatePublished { get; set; }
        public long FileSize { get; set; }
        public string FileType { get; set; }
        public const Int32 FileSizeUnknown = -1;
        public const string FileTypeUnknown = "";
        public const string FileTypeMp3 = "audio/mpeg";
        public const string FileTypeVideo = "video/m4v";
        public bool IsVideo
        {
            get
            {
                return
                    this.FileType.Equals(FileTypeVideo, StringComparison.OrdinalIgnoreCase)
                    || (!string.IsNullOrEmpty(this.DownloadUrl.OriginalString) && Path.GetExtension(this.DownloadUrl.OriginalString).Equals(".m4v", StringComparison.OrdinalIgnoreCase));
            }
        }

        #region IEquatable<Episode> Members

        /// <summary>
        /// Episode instance are equal if thier uniqueIds are equal
        /// </summary>
        public override bool Equals(object obj)
        {
            var other = obj as Episode;
            if (other == null)
                return false;

            return this.Equals(other);
        }
        public bool Equals(Episode other)
        {
            return
                this.UniqueId.Equals(other.UniqueId, StringComparison.OrdinalIgnoreCase);
        }
        public static bool operator ==(Episode a, Episode b)
        {
            if (System.Object.ReferenceEquals(a, b))
                return true;

            if (((object)a == null) || ((object)b == null))
                return false;

            return ((Episode)a).Equals((Episode)b);
        }
        public static bool operator !=(Episode a, Episode b)
        {
            return !Episode.Equals(a, b);
        }

        public override int GetHashCode()
        {
            return this.UniqueId.GetHashCode();
        }
        #endregion
    }

}
